<?php
/**
 * Created by PhpStorm.
 * User: FDG367
 * Date: 2017/9/27
 * Time: 16:01
 */
namespace common\widgets;

use Yii;
use yii\bootstrap\Html;
use \yii\bootstrap\Widget;
use yii\helpers\ArrayHelper;
use yii\web\JsExpression;

class SysCity extends Widget{
	public $province_id;
	public $city_id;
	public $area_id;



	public function init(){
		parent::init();
	}

	public function run()
	{
		//加载数据
		$filename = __DIR__ . '/../../backend/config/sys_city.php';
		$sys_city = file_get_contents($filename);

		//js
		$jsScript = <<<EOF
		var sys_city = {$sys_city};
		function getprovince(province_id=''){
				var city_id = "{$this->city_id}";
				var city_html = '<option value="0">请选择</option>';
				sys_city.forEach(function (item) {
					if(province_id == item.id){
						var city_val = item.son_val;
						if(city_val != '' && city_val != null){
						    city_val.forEach(function (item1) {
							city_html += '<option '+(city_id == item1.id ? 'selected' : '')+' value="'+item1.id+'">'+item1.name+'</option>';
						});
						}
					}
				});
				$('#city_id').html(city_html);
				$('#area_id').html('<option value="0">请选择</option>');
				getcity(city_id);
		}


		function getcity(city_id){
			var province_id = $('#province_id').val();
					var area_id = "{$this->area_id}";

				var area_html = '<option value="0">请选择</option>';
				sys_city.forEach(function (item) {
					if(province_id == item.id){
						var city_val = item.son_val;
						if(city_val != '' && city_val != null){
						city_val.forEach(function (item1) {
							if(city_id == item1.id){
								var area_val = item1.son_val;
								if(area_val != '' && area_val != null){
								    area_val.forEach(function (item2) {
									area_html += '<option '+(area_id == item2.id ? 'selected' : '')+' value="'+item2.id+'">'+item2.name+'</option>';
								});
								}
							}
						});
						}
					}
				});
				$('#area_id').html(area_html);
		}
		$(function() {
			var province_id	= "{$this->province_id}";
			if(province_id)
				getprovince(province_id);


			$('#province_id').on('change',function(){
				getprovince($(this).val());
			});

			$('#city_id').on('change',function(){
				getcity($(this).val());
			});
		});
EOF;

		//处理数据
		$view = $this->getView();
		$view->registerJs((new JsExpression($jsScript)));
		return  '<div class="item">'.Html::dropDownList('province_id',null,ArrayHelper::map(json_decode($sys_city),'id', 'name'), [
			'id'=>'province_id',
			'class'=>'province_id province_ext_1 province_ext_2',
			'prompt' => '请选择省',
			'options'=>[
				$this->province_id=>[
					'Selected'=>true
				]
			]
		]).'</div>&nbsp;&nbsp;<div class="item">'.Html::dropDownList('city_id',null,[], [
			'id'=>'city_id',
			'class'=>'city_id city_ext_1 city_ext_2',
			'prompt' => '请选择市',
		]).'</div>&nbsp;&nbsp;<div class="item">'.Html::dropDownList('area_id',null,[], [
			'id'=>'area_id',
			'class'=>'area_id area_ext_1 area_ext_2',
			'prompt' => '请选择区',
		]).'</div>';
	}
}